package networkSimulation.simulation.virus

import java.io.IOException
import monitor.VirusSpreadMonitor
import java.lang.reflect.Method
import networkSimulation.visualisation.gui.VisualUIFrame
import simulation.Virus
import util.Random
import networkSimulation.framework.Edge

/**
 * Runnable class for a Virus simulation
 *
 * @author  Tim Caljé
 *          Jeroen De Raedt
 *          Jorick Van Aken
 */
object Run {

  def main(args: Array[String]) {
    try {
      val graph = createGraph(100)
      val virus = new Virus(graph)
      var spreadMonitor = new VirusSpreadMonitor[Method, Virus#Pub]("Virus Spread monitor", graph)
      virus.subscribe(spreadMonitor)
      virus.storeGraph("test.xml")
      virus.load("test.xml")
      var outputpath = "out\\"
      virus.startSimulation(20,outputpath)
      new VisualUIFrame(outputpath)
      new VisualUIFrame(outputpath+"graph")
    } catch {
      case e: IllegalArgumentException => println(e.getMessage)
      case e: IllegalStateException    => println("Illegal state exception: " + e.getMessage)
      case e: IOException              => println("IO exception: " + e.getMessage)
      case e: NullPointerException     => println("NullPointerException: " + e.getMessage)
      case e: Exception                => println("Unknown error: " + e.getMessage)
    }
  }

  /**
   * Create a graph for the virus-simulation
   */
  private def createGraph(nbNodes : Int) : VirusGraph = {

    val graph = new VirusGraph

    for (i <- 0 until nbNodes){
      graph.addNode(new InfectablePerson("n" + i))
    }

    for (i <- 0 until nbNodes*2){
      graph.addEdge(new Edge[InfectablePerson](graph.nodes.apply(new Random().nextInt(nbNodes)),graph.nodes.apply(new Random().nextInt(nbNodes))))
    }
      
    graph
  }

}